package practices; /**
 * @program: Java_Study
 * @author: Xiaofan
 * @createTime: 2021-06-19 15:21
 * @description: Functions of this class is
 **/
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;

public class Client2 {
    Socket socket;
    BufferedReader in;
    PrintWriter out;

    public Client2() {

        try {
            socket = new Socket("127.0.0.1", 10000);
        } catch (UnknownHostException e1) {
            e1.printStackTrace();
        } catch (IOException e1) {
            e1.printStackTrace();
        }

        new Thread(){
            public void run(){
                try {
                    BufferedReader line = new BufferedReader(new InputStreamReader(System.in));
                    out = new PrintWriter(socket.getOutputStream(), true);
                    String str = null;
                    while((str= line.readLine()) != null){
                        out.println(str);
                    }
                    line.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }finally{
                    out.close();
                }
            }
        }.start();

        new Thread(){
            public void run(){
                try {
                    in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
                    String str = null;
                    while((str = in.readLine()) != null){
                        System.out.println("接收到信息:" + str);
                    }
                    in.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }.start();
    }

    public static void main(String[] args) {
        new Client2();
    }
}